<!DOCTYPE HTML>
<html lang="en" >
    <!-- Start book Python数据分析课程讲义 -->
    <head>
        <!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>Matplotlib绘图 | Python数据分析课程讲义</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        <meta name="author" content="BigCat">
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-tbfed-pagefooter/footer.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../../file/part04/4.2.html" />
    
    
    <link rel="prev" href="../../file/part04/4.html" />
    

        <!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
        
    <div class="book"
        data-level="4.1"
        data-chapter-title="Matplotlib绘图"
        data-filepath="file/part04/4.1.md"
        data-basepath="../.."
        data-revision="Thu Apr 27 2017 00:50:19 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        传智播客Python学院数据分析
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="file/part01/1.html">
            
                
                    <a href="../../file/part01/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        一、工作环境准备及数据分析建模理论基础
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="file/part01/1.1.html">
            
                
                    <a href="../../file/part01/1.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        Python 3.x新特性和编码回顾
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="file/part01/1.2.html">
            
                
                    <a href="../../file/part01/1.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        DIKW模型与数据工程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="file/part01/1.3.html">
            
                
                    <a href="../../file/part01/1.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        数据分析建模理论基础
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="file/part02/2.html">
            
                
                    <a href="../../file/part02/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        二、科学计算工具NumPy
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="file/part02/2.1.html">
            
                
                    <a href="../../file/part02/2.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        ndarray的创建与数据类型
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="file/part02/2.2.html">
            
                
                    <a href="../../file/part02/2.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        ndarray的矩阵处理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="file/part02/2.3.html">
            
                
                    <a href="../../file/part02/2.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        ndarray的元素处理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="file/part02/2.4.html">
            
                
                    <a href="../../file/part02/2.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        实战案例：2016美国总统大选民意调查统计
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="file/part03/3.html">
            
                
                    <a href="../../file/part03/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        三、数据分析工具Pandas
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="file/part03/3.1.html">
            
                
                    <a href="../../file/part03/3.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        Pandas的数据结构
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="file/part03/3.2.html">
            
                
                    <a href="../../file/part03/3.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        Pandas的索引操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="file/part03/3.3.html">
            
                
                    <a href="../../file/part03/3.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        Pandas的对齐运算
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="file/part03/3.4.html">
            
                
                    <a href="../../file/part03/3.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        Pandas的函数应用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="file/part03/3.5.html">
            
                
                    <a href="../../file/part03/3.5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                        Pandas的层级索引
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="file/part03/3.6.html">
            
                
                    <a href="../../file/part03/3.6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.6.</b>
                        
                        Pandas统计计算和描述
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.7" data-path="file/part03/3.7.html">
            
                
                    <a href="../../file/part03/3.7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.7.</b>
                        
                        Pandas分组与聚合
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.8" data-path="file/part03/3.8.html">
            
                
                    <a href="../../file/part03/3.8.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.8.</b>
                        
                        数据清洗、合并、转化和重构
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.9" data-path="file/part03/3.9.html">
            
                
                    <a href="../../file/part03/3.9.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.9.</b>
                        
                        聚类模型 -- K-Means介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.10" data-path="file/part03/3.10.html">
            
                
                    <a href="../../file/part03/3.10.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.10.</b>
                        
                        实战案例：全球食品数据分析
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="file/part04/4.html">
            
                
                    <a href="../../file/part04/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        四、数据可视化工具
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="4.1" data-path="file/part04/4.1.html">
            
                
                    <a href="../../file/part04/4.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        Matplotlib绘图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="file/part04/4.2.html">
            
                
                    <a href="../../file/part04/4.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        Seaborn绘图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="file/part04/4.3.html">
            
                
                    <a href="../../file/part04/4.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        Bokeh绘图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="file/part04/4.4.html">
            
                
                    <a href="../../file/part04/4.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        实战案例：世界高峰数据可视化
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="file/part06/6.html">
            
                
                    <a href="../../file/part06/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        五、自然语言处理NLTK
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="file/part06/6.1.html">
            
                
                    <a href="../../file/part06/6.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        NLTK与自然语言处理基础
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="file/part06/6.2.html">
            
                
                    <a href="../../file/part06/6.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        jieba分词
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="file/part06/6.3.html">
            
                
                    <a href="../../file/part06/6.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        情感分析
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="file/part06/6.4.html">
            
                
                    <a href="../../file/part06/6.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.4.</b>
                        
                        文本相似度和分类
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="file/part06/6.6.html">
            
                
                    <a href="../../file/part06/6.6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.5.</b>
                        
                        实战案例：微博情感分析
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../../" >Python数据分析课程讲义</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <p><img src="../images/matplotlib_logo.svg" alt=""></p>
<p>Matplotlib &#x662F;&#x4E00;&#x4E2A; Python &#x7684; 2D&#x7ED8;&#x56FE;&#x5E93;&#xFF0C;&#x901A;&#x8FC7; Matplotlib&#xFF0C;&#x5F00;&#x53D1;&#x8005;&#x53EF;&#x4EE5;&#x4EC5;&#x9700;&#x8981;&#x51E0;&#x884C;&#x4EE3;&#x7801;&#xFF0C;&#x4FBF;&#x53EF;&#x4EE5;&#x751F;&#x6210;&#x7ED8;&#x56FE;&#xFF0C;&#x76F4;&#x65B9;&#x56FE;&#xFF0C;&#x529F;&#x7387;&#x8C31;&#xFF0C;&#x6761;&#x5F62;&#x56FE;&#xFF0C;&#x9519;&#x8BEF;&#x56FE;&#xFF0C;&#x6563;&#x70B9;&#x56FE;&#x7B49;&#x3002;</p>
<p><a href="http://matplotlib.org" target="_blank">http://matplotlib.org</a></p>
<ul>
<li><p>&#x7528;&#x4E8E;&#x521B;&#x5EFA;&#x51FA;&#x7248;&#x8D28;&#x91CF;&#x56FE;&#x8868;&#x7684;&#x7ED8;&#x56FE;&#x5DE5;&#x5177;&#x5E93;</p>
</li>
<li><p>&#x76EE;&#x7684;&#x662F;&#x4E3A;Python&#x6784;&#x5EFA;&#x4E00;&#x4E2A;Matlab&#x5F0F;&#x7684;&#x7ED8;&#x56FE;&#x63A5;&#x53E3;</p>
</li>
<li><p><code>import matplotlib.pyplot as plt</code></p>
</li>
<li><p>pyploy&#x6A21;&#x5757;&#x5305;&#x542B;&#x4E86;&#x5E38;&#x7528;&#x7684;matplotlib API&#x51FD;&#x6570;</p>
</li>
</ul>
<p><img src="../images/matplotlib.png" alt=""></p>
<h1 id="figure">figure</h1>
<ul>
<li><p>Matplotlib&#x7684;&#x56FE;&#x50CF;&#x5747;&#x4F4D;&#x4E8E;figure&#x5BF9;&#x8C61;&#x4E2D;</p>
</li>
<li><p>&#x521B;&#x5EFA;figure&#xFF1A;<code>fig = plt.figure()</code></p>
</li>
</ul>
<p>&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-comment"># &#x5F15;&#x5165;matplotlib&#x5305;</span>
<span class="hljs-keyword">import</span> matplotlib.pyplot <span class="hljs-keyword">as</span> plt
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np

%matplotlib inline <span class="hljs-comment">#&#x5728;jupyter notebook &#x91CC;&#x9700;&#x8981;&#x4F7F;&#x7528;&#x8FD9;&#x4E00;&#x53E5;&#x547D;&#x4EE4;</span>

<span class="hljs-comment"># &#x521B;&#x5EFA;figure&#x5BF9;&#x8C61;</span>
fig = plt.figure()
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-python">&lt;matplotlib.figure.Figure at <span class="hljs-number">0x11a2dd7b8</span>&gt;
</code></pre>
<h1 id="subplot">subplot</h1>
<h4 id="figaddsubplota-b-c">fig.add_subplot(a, b, c)</h4>
<ul>
<li><p>a,b &#x8868;&#x793A;&#x5C06;fig&#x5206;&#x5272;&#x6210; a*b &#x7684;&#x533A;&#x57DF;</p>
</li>
<li><p>c &#x8868;&#x793A;&#x5F53;&#x524D;&#x9009;&#x4E2D;&#x8981;&#x64CD;&#x4F5C;&#x7684;&#x533A;&#x57DF;&#xFF0C;</p>
</li>
<li><p>&#x6CE8;&#x610F;&#xFF1A;&#x4ECE;1&#x5F00;&#x59CB;&#x7F16;&#x53F7;&#xFF08;&#x4E0D;&#x662F;&#x4ECE;0&#x5F00;&#x59CB;&#xFF09;</p>
</li>
<li><p>plot &#x7ED8;&#x56FE;&#x7684;&#x533A;&#x57DF;&#x662F;&#x6700;&#x540E;&#x4E00;&#x6B21;&#x6307;&#x5B9A;subplot&#x7684;&#x4F4D;&#x7F6E; (jupyter notebook&#x91CC;&#x4E0D;&#x80FD;&#x6B63;&#x786E;&#x663E;&#x793A;)</p>
</li>
</ul>
<p>&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-comment"># &#x6307;&#x5B9A;&#x5207;&#x5206;&#x533A;&#x57DF;&#x7684;&#x4F4D;&#x7F6E;</span>
ax1 = fig.add_subplot(<span class="hljs-number">2</span>,<span class="hljs-number">2</span>,<span class="hljs-number">1</span>)
ax2 = fig.add_subplot(<span class="hljs-number">2</span>,<span class="hljs-number">2</span>,<span class="hljs-number">2</span>)
ax3 = fig.add_subplot(<span class="hljs-number">2</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>)
ax4 = fig.add_subplot(<span class="hljs-number">2</span>,<span class="hljs-number">2</span>,<span class="hljs-number">4</span>)

<span class="hljs-comment"># &#x5728;subplot&#x4E0A;&#x4F5C;&#x56FE;</span>
random_arr = np.random.randn(<span class="hljs-number">100</span>)
<span class="hljs-comment">#print random_arr</span>

<span class="hljs-comment"># &#x9ED8;&#x8BA4;&#x662F;&#x5728;&#x6700;&#x540E;&#x4E00;&#x6B21;&#x4F7F;&#x7528;subplot&#x7684;&#x4F4D;&#x7F6E;&#x4E0A;&#x4F5C;&#x56FE;&#xFF0C;&#x4F46;&#x662F;&#x5728;jupyter notebook &#x91CC;&#x53EF;&#x80FD;&#x663E;&#x793A;&#x6709;&#x8BEF;</span>
plt.plot(random_arr)

<span class="hljs-comment"># &#x53EF;&#x4EE5;&#x6307;&#x5B9A;&#x5728;&#x67D0;&#x4E2A;&#x6216;&#x591A;&#x4E2A;subplot&#x4F4D;&#x7F6E;&#x4E0A;&#x4F5C;&#x56FE;</span>
<span class="hljs-comment"># ax1 = fig.plot(random_arr)</span>
<span class="hljs-comment"># ax2 = fig.plot(random_arr)</span>
<span class="hljs-comment"># ax3 = fig.plot(random_arr)</span>

<span class="hljs-comment"># &#x663E;&#x793A;&#x7ED8;&#x56FE;&#x7ED3;&#x679C;</span>
plt.show()
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<p><img src="../images/plt_01.png" alt=""></p>
<h1 id="&#x76F4;&#x65B9;&#x56FE;&#xFF1A;hist">&#x76F4;&#x65B9;&#x56FE;&#xFF1A;hist</h1>
<p>&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> matplotlib.pyplot <span class="hljs-keyword">as</span> plt
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np

plt.hist(np.random.randn(<span class="hljs-number">100</span>), bins=<span class="hljs-number">10</span>, color=<span class="hljs-string">&apos;b&apos;</span>, alpha=<span class="hljs-number">0.3</span>)
plt.show()
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<p><img src="../images/plt_02.png" alt=""></p>
<h1 id="&#x6563;&#x70B9;&#x56FE;&#xFF1A;scatter">&#x6563;&#x70B9;&#x56FE;&#xFF1A;scatter</h1>
<p>&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> matplotlib.pyplot <span class="hljs-keyword">as</span> plt
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np

<span class="hljs-comment"># &#x7ED8;&#x5236;&#x6563;&#x70B9;&#x56FE;</span>
x = np.arange(<span class="hljs-number">50</span>)
y = x + <span class="hljs-number">5</span> * np.random.rand(<span class="hljs-number">50</span>)
plt.scatter(x, y)
plt.show()
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;
<img src="../images/plt_03.png" alt=""></p>
<h1 id="&#x67F1;&#x72B6;&#x56FE;&#xFF1A;bar">&#x67F1;&#x72B6;&#x56FE;&#xFF1A;bar</h1>
<p>&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> matplotlib.pyplot <span class="hljs-keyword">as</span> plt
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np

<span class="hljs-comment"># &#x67F1;&#x72B6;&#x56FE;</span>
x = np.arange(<span class="hljs-number">5</span>)
y1, y2 = np.random.randint(<span class="hljs-number">1</span>, <span class="hljs-number">25</span>, size=(<span class="hljs-number">2</span>, <span class="hljs-number">5</span>))
width = <span class="hljs-number">0.25</span>
ax = plt.subplot(<span class="hljs-number">1</span>,<span class="hljs-number">1</span>,<span class="hljs-number">1</span>)
ax.bar(x, y1, width, color=<span class="hljs-string">&apos;r&apos;</span>)
ax.bar(x+width, y2, width, color=<span class="hljs-string">&apos;g&apos;</span>)
ax.set_xticks(x+width)
ax.set_xticklabels([<span class="hljs-string">&apos;a&apos;</span>, <span class="hljs-string">&apos;b&apos;</span>, <span class="hljs-string">&apos;c&apos;</span>, <span class="hljs-string">&apos;d&apos;</span>, <span class="hljs-string">&apos;e&apos;</span>])
plt.show()
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<p><img src="../images/plt_04.png" alt=""></p>
<h1 id="&#x77E9;&#x9635;&#x7ED8;&#x56FE;&#xFF1A;pltimshow">&#x77E9;&#x9635;&#x7ED8;&#x56FE;&#xFF1A;plt.imshow()</h1>
<ul>
<li>&#x6DF7;&#x6DC6;&#x77E9;&#x9635;&#xFF0C;&#x4E09;&#x4E2A;&#x7EF4;&#x5EA6;&#x7684;&#x5173;&#x7CFB; </li>
</ul>
<p>&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> matplotlib.pyplot <span class="hljs-keyword">as</span> plt
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np

<span class="hljs-comment"># &#x77E9;&#x9635;&#x7ED8;&#x56FE;</span>
m = np.random.rand(<span class="hljs-number">10</span>,<span class="hljs-number">10</span>)
print(m)
plt.imshow(m, interpolation=<span class="hljs-string">&apos;nearest&apos;</span>, cmap=plt.cm.ocean)
plt.colorbar()
plt.show()
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-python">[[ <span class="hljs-number">0.92859942</span>  <span class="hljs-number">0.84162134</span>  <span class="hljs-number">0.37814667</span>  <span class="hljs-number">0.46401549</span>  <span class="hljs-number">0.93935737</span>  <span class="hljs-number">0.0344159</span>
   <span class="hljs-number">0.56358375</span>  <span class="hljs-number">0.75977745</span>  <span class="hljs-number">0.87983192</span>  <span class="hljs-number">0.22818774</span>]
 [ <span class="hljs-number">0.88216959</span>  <span class="hljs-number">0.43369207</span>  <span class="hljs-number">0.1303902</span>   <span class="hljs-number">0.98446182</span>  <span class="hljs-number">0.59474031</span>  <span class="hljs-number">0.04414217</span>
   <span class="hljs-number">0.86534444</span>  <span class="hljs-number">0.34919228</span>  <span class="hljs-number">0.53950028</span>  <span class="hljs-number">0.89165269</span>]
 [ <span class="hljs-number">0.52919761</span>  <span class="hljs-number">0.87408715</span>  <span class="hljs-number">0.097871</span>    <span class="hljs-number">0.78348534</span>  <span class="hljs-number">0.09354791</span>  <span class="hljs-number">0.3186</span>
   <span class="hljs-number">0.25978432</span>  <span class="hljs-number">0.48340623</span>  <span class="hljs-number">0.1107699</span>   <span class="hljs-number">0.14065592</span>]
 [ <span class="hljs-number">0.90834516</span>  <span class="hljs-number">0.42377475</span>  <span class="hljs-number">0.73042695</span>  <span class="hljs-number">0.51596826</span>  <span class="hljs-number">0.14154431</span>  <span class="hljs-number">0.22165693</span>
   <span class="hljs-number">0.64705882</span>  <span class="hljs-number">0.78062873</span>  <span class="hljs-number">0.55036304</span>  <span class="hljs-number">0.40874584</span>]
 [ <span class="hljs-number">0.98853697</span>  <span class="hljs-number">0.46762114</span>  <span class="hljs-number">0.69973423</span>  <span class="hljs-number">0.7910757</span>   <span class="hljs-number">0.63700306</span>  <span class="hljs-number">0.68793919</span>
   <span class="hljs-number">0.28685306</span>  <span class="hljs-number">0.3473426</span>   <span class="hljs-number">0.17011744</span>  <span class="hljs-number">0.18812329</span>]
 [ <span class="hljs-number">0.73688943</span>  <span class="hljs-number">0.58004874</span>  <span class="hljs-number">0.03146167</span>  <span class="hljs-number">0.08875797</span>  <span class="hljs-number">0.32930191</span>  <span class="hljs-number">0.87314734</span>
   <span class="hljs-number">0.50757536</span>  <span class="hljs-number">0.8667078</span>   <span class="hljs-number">0.8423364</span>   <span class="hljs-number">0.99079049</span>]
 [ <span class="hljs-number">0.37660356</span>  <span class="hljs-number">0.63667774</span>  <span class="hljs-number">0.78111565</span>  <span class="hljs-number">0.25598593</span>  <span class="hljs-number">0.38437628</span>  <span class="hljs-number">0.95771051</span>
   <span class="hljs-number">0.01922366</span>  <span class="hljs-number">0.37020219</span>  <span class="hljs-number">0.51020305</span>  <span class="hljs-number">0.05365718</span>]
 [ <span class="hljs-number">0.87588452</span>  <span class="hljs-number">0.56494761</span>  <span class="hljs-number">0.67320078</span>  <span class="hljs-number">0.46870376</span>  <span class="hljs-number">0.66139913</span>  <span class="hljs-number">0.55072149</span>
   <span class="hljs-number">0.51328222</span>  <span class="hljs-number">0.64817353</span>  <span class="hljs-number">0.198525</span>    <span class="hljs-number">0.18105368</span>]
 [ <span class="hljs-number">0.86038137</span>  <span class="hljs-number">0.55914088</span>  <span class="hljs-number">0.55240021</span>  <span class="hljs-number">0.15260395</span>  <span class="hljs-number">0.4681218</span>   <span class="hljs-number">0.28863395</span>
   <span class="hljs-number">0.6614597</span>   <span class="hljs-number">0.69015592</span>  <span class="hljs-number">0.46583629</span>  <span class="hljs-number">0.15086562</span>]
 [ <span class="hljs-number">0.01373772</span>  <span class="hljs-number">0.30514083</span>  <span class="hljs-number">0.69804049</span>  <span class="hljs-number">0.5014782</span>   <span class="hljs-number">0.56855904</span>  <span class="hljs-number">0.14889117</span>
   <span class="hljs-number">0.87596848</span>  <span class="hljs-number">0.29757133</span>  <span class="hljs-number">0.76062891</span>  <span class="hljs-number">0.03678431</span>]]
</code></pre>
<p><img src="../images/plt_05.png" alt=""></p>
<h2 id="pltsubplots">plt.subplots()</h2>
<ul>
<li><p>&#x540C;&#x65F6;&#x8FD4;&#x56DE;&#x65B0;&#x521B;&#x5EFA;&#x7684;<code>figure</code>&#x548C;<code>subplot</code>&#x5BF9;&#x8C61;&#x6570;&#x7EC4;</p>
</li>
<li><p>&#x751F;&#x6210;2&#x884C;2&#x5217;subplot:<code>fig, subplot_arr = plt.subplots(2,2)</code></p>
</li>
<li><p>&#x5728;jupyter&#x91CC;&#x53EF;&#x4EE5;&#x6B63;&#x5E38;&#x663E;&#x793A;&#xFF0C;&#x63A8;&#x8350;&#x4F7F;&#x7528;&#x8FD9;&#x79CD;&#x65B9;&#x5F0F;&#x521B;&#x5EFA;&#x591A;&#x4E2A;&#x56FE;&#x8868;</p>
</li>
</ul>
<p>&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> matplotlib.pyplot <span class="hljs-keyword">as</span> plt
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np

fig, subplot_arr = plt.subplots(<span class="hljs-number">2</span>,<span class="hljs-number">2</span>)
<span class="hljs-comment"># bins &#x4E3A;&#x663E;&#x793A;&#x4E2A;&#x6570;&#xFF0C;&#x4E00;&#x822C;&#x5C0F;&#x4E8E;&#x7B49;&#x4E8E;&#x6570;&#x503C;&#x4E2A;&#x6570;</span>
subplot_arr[<span class="hljs-number">1</span>,<span class="hljs-number">0</span>].hist(np.random.randn(<span class="hljs-number">100</span>), bins=<span class="hljs-number">10</span>, color=<span class="hljs-string">&apos;b&apos;</span>, alpha=<span class="hljs-number">0.3</span>)
plt.show()
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;
<img src="../images/plt_06.png" alt=""></p>
<h2 id="&#x989C;&#x8272;&#x3001;&#x6807;&#x8BB0;&#x3001;&#x7EBF;&#x578B;">&#x989C;&#x8272;&#x3001;&#x6807;&#x8BB0;&#x3001;&#x7EBF;&#x578B;</h2>
<ul>
<li>ax.plot(x, y, &#x2018;r--&#x2019;)</li>
</ul>
<blockquote>
<p>&#x7B49;&#x4EF7;&#x4E8E;ax.plot(x, y, linestyle=&#x2018;--&#x2019;, color=&#x2018;r&#x2019;)</p>
</blockquote>
<p>&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> matplotlib.pyplot <span class="hljs-keyword">as</span> plt
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np

fig, axes = plt.subplots(<span class="hljs-number">2</span>)
axes[<span class="hljs-number">0</span>].plot(np.random.randint(<span class="hljs-number">0</span>, <span class="hljs-number">100</span>, <span class="hljs-number">50</span>), <span class="hljs-string">&apos;ro--&apos;</span>)
<span class="hljs-comment"># &#x7B49;&#x4EF7;</span>
axes[<span class="hljs-number">1</span>].plot(np.random.randint(<span class="hljs-number">0</span>, <span class="hljs-number">100</span>, <span class="hljs-number">50</span>), color=<span class="hljs-string">&apos;r&apos;</span>, linestyle=<span class="hljs-string">&apos;dashed&apos;</span>, marker=<span class="hljs-string">&apos;o&apos;</span>)
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<p><code>[&lt;matplotlib.lines.Line2D at 0x11a901e80&gt;]</code></p>
<p><img src="../images/plt_07.png" alt=""></p>
<ul>
<li>&#x5E38;&#x7528;&#x7684;&#x989C;&#x8272;&#x3001;&#x6807;&#x8BB0;&#x3001;&#x7EBF;&#x578B;</li>
</ul>
<p><img src="../images/plt_color.png" alt=""></p>
<p><img src="../images/plt_marker.png" alt=""></p>
<p><img src="../images/plt_linestyle.png" alt=""></p>
<h2 id="&#x523B;&#x5EA6;&#x3001;&#x6807;&#x7B7E;&#x3001;&#x56FE;&#x4F8B;">&#x523B;&#x5EA6;&#x3001;&#x6807;&#x7B7E;&#x3001;&#x56FE;&#x4F8B;</h2>
<ul>
<li><p>&#x8BBE;&#x7F6E;&#x523B;&#x5EA6;&#x8303;&#x56F4;</p>
<blockquote>
<p>plt.xlim(), plt.ylim()</p>
<p>ax.set_xlim(), ax.set_ylim()</p>
</blockquote>
</li>
<li><p>&#x8BBE;&#x7F6E;&#x663E;&#x793A;&#x7684;&#x523B;&#x5EA6;</p>
<blockquote>
<p>plt.xticks(), plt.yticks()</p>
<p>ax.set_xticks(), ax.set_yticks()</p>
</blockquote>
</li>
<li><p>&#x8BBE;&#x7F6E;&#x523B;&#x5EA6;&#x6807;&#x7B7E;</p>
<blockquote>
<p>ax.set_xticklabels(), ax.set_yticklabels()</p>
</blockquote>
</li>
<li><p>&#x8BBE;&#x7F6E;&#x5750;&#x6807;&#x8F74;&#x6807;&#x7B7E;</p>
<blockquote>
<p>ax.set_xlabel(), ax.set_ylabel()</p>
</blockquote>
</li>
<li><p>&#x8BBE;&#x7F6E;&#x6807;&#x9898;</p>
<blockquote>
<p>ax.set_title()</p>
</blockquote>
</li>
<li><p>&#x56FE;&#x4F8B;</p>
<blockquote>
<p>ax.plot(label=&#x2018;legend&#x2019;)</p>
<p>ax.legend(), plt.legend()</p>
<p>loc=&#x2018;best&#x2019;&#xFF1A;&#x81EA;&#x52A8;&#x9009;&#x62E9;&#x653E;&#x7F6E;&#x56FE;&#x4F8B;&#x6700;&#x4F73;&#x4F4D;&#x7F6E;</p>
</blockquote>
</li>
</ul>
<p>&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> matplotlib.pyplot <span class="hljs-keyword">as</span> plt
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np

fig, ax = plt.subplots(<span class="hljs-number">1</span>)
ax.plot(np.random.randn(<span class="hljs-number">1000</span>).cumsum(), label=<span class="hljs-string">&apos;line0&apos;</span>)

<span class="hljs-comment"># &#x8BBE;&#x7F6E;&#x523B;&#x5EA6;</span>
<span class="hljs-comment">#plt.xlim([0,500])</span>
ax.set_xlim([<span class="hljs-number">0</span>, <span class="hljs-number">800</span>])

<span class="hljs-comment"># &#x8BBE;&#x7F6E;&#x663E;&#x793A;&#x7684;&#x523B;&#x5EA6;</span>
<span class="hljs-comment">#plt.xticks([0,500])</span>
ax.set_xticks(range(<span class="hljs-number">0</span>,<span class="hljs-number">500</span>,<span class="hljs-number">100</span>))

<span class="hljs-comment"># &#x8BBE;&#x7F6E;&#x523B;&#x5EA6;&#x6807;&#x7B7E;</span>
ax.set_yticklabels([<span class="hljs-string">&apos;Jan&apos;</span>, <span class="hljs-string">&apos;Feb&apos;</span>, <span class="hljs-string">&apos;Mar&apos;</span>])

<span class="hljs-comment"># &#x8BBE;&#x7F6E;&#x5750;&#x6807;&#x8F74;&#x6807;&#x7B7E;</span>
ax.set_xlabel(<span class="hljs-string">&apos;Number&apos;</span>)
ax.set_ylabel(<span class="hljs-string">&apos;Month&apos;</span>)

<span class="hljs-comment"># &#x8BBE;&#x7F6E;&#x6807;&#x9898;</span>
ax.set_title(<span class="hljs-string">&apos;Example&apos;</span>)

<span class="hljs-comment"># &#x56FE;&#x4F8B;</span>
ax.plot(np.random.randn(<span class="hljs-number">1000</span>).cumsum(), label=<span class="hljs-string">&apos;line1&apos;</span>)
ax.plot(np.random.randn(<span class="hljs-number">1000</span>).cumsum(), label=<span class="hljs-string">&apos;line2&apos;</span>)
ax.legend()
ax.legend(loc=<span class="hljs-string">&apos;best&apos;</span>)
<span class="hljs-comment">#plt.legend()</span>
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;
<code>&lt;matplotlib.legend.Legend at 0x11a4061d0&gt;</code>
<img src="../images/plt_08.png" alt=""></p>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; BigCat all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x300C;Revision Time:
2017-04-24 22:44:11&#x300D;
</span></footer>
                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../../file/part04/4.html" class="navigation navigation-prev " aria-label="Previous page: 四、数据可视化工具"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../../file/part04/4.2.html" class="navigation navigation-next " aria-label="Next page: Seaborn绘图"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../../gitbook/app.js"></script>

    
    <script src="../../gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-livereload/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"disqus":{"shortName":"gitbookuse"},"github":{"url":"https://github.com/dododream"},"search-pro":{"cutWordLib":"nodejieba","defineWord":["gitbook-use"]},"sharing":{"weibo":true,"facebook":true,"twitter":true,"google":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"tbfed-pagefooter":{"copyright":"Copyright © BigCat","modify_label":"「Revision Time:","modify_format":"YYYY-MM-DD HH:mm:ss」"},"baidu":{"token":"ff100361cdce95dd4c8fb96b4009f7bc"},"sitemap":{"hostname":"http://www.treenewbee.top"},"donate":{"wechat":"http://weixin.png","alipay":"http://alipay.png","title":"","button":"赏","alipayText":"支付宝打赏","wechatText":"微信打赏"},"edit-link":{"base":"https://github.com/dododream/edit","label":"Edit This Page"},"splitter":{},"toggle-chapters":{},"highlight":{},"fontsettings":{"theme":"white","family":"sans","size":2},"livereload":{}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
    </body>
    <!-- End of book Python数据分析课程讲义 -->
</html>
